{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Condicionales\n",
    "\n",
    "En Julia, la sintaxis\n",
    "\n",
    "```julia\n",
    "if *condición 1*\n",
    "    *opción 1*\n",
    "elseif *condición 2*\n",
    "    *opción 2*\n",
    "else\n",
    "    *opción 3*\n",
    "end\n",
    "```\n",
    "\n",
    "Nos permite eventualmente evaluar una de nuestras opciones.\n",
    "<br><br>\n",
    "Por ejemplo, tal vez queremos implementar la prueba de FizzBuzz: Dado un número N, imprime \"Fizz\" si N es divisible entre 3, \"Buzz\" si N es divisible entre 5, y \"FizzBuzz\" si N es divisible entre ambos 3 y 5. En cualquier otro caso, imprimo el número mismo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "N = "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "if (N % 3 == 0) & (N % 5 == 0)\n",
    "    println(\"FizzBuzz\")\n",
    "elseif N % 3 == 0\n",
    "    println(\"Fizz\")\n",
    "elseif N % 5 == 0\n",
    "    println(\"Buzz\")\n",
    "else\n",
    "    println(N)\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ahora digamos que queremos regresar el mayor número de ambos. Escoge tus propios x y y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = \n",
    "y = "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "if x > y\n",
    "    x\n",
    "else\n",
    "    y\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Para el último bloque, podemos usar el operador ternario, con la sintaxis\n",
    "\n",
    "```julia\n",
    "a ? b : c\n",
    "```\n",
    "\n",
    "que equivale a\n",
    "\n",
    "```julia\n",
    "if a\n",
    "    b\n",
    "else\n",
    "    c\n",
    "end\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "(x > y) ? x : y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Un truco relacionado es la evaluación de corto-circuito \n",
    "\n",
    "```julia\n",
    "a && b\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "(x > y) && println(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "(x < y) && println(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Cuando escribimos `a && b`, `b` se ejecuta sólo si `a` se evalúa a `true`.\n",
    "<br>\n",
    "Si `a` se evalúa a `false`, la expresión `a && b` regresa `false`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Ejercicios\n",
    "\n",
    "5.1 Reescribe FizzBuzz sin usar `elseif`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.2 Reescribe FizzBuzz usando el operador ternario."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.1",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
